<?php
    $username = $_GET["username"];
    if ($username){
        try{
            $conn = mysql_connect("127.0.0.1", "root", "");
            if ($conn){
                mysql_select_db("dbOpenFire", $conn);
                $foundUsers = mysql_query("SELECT username FROM dbOpenFire.ofUser WHERE username='$username'", $conn);
                $alreadyWrittenData = mysql_query("SELECT username FROM dbOpenFire.ofVCard WHERE username='$username'");
                if (mysql_fetch_assoc($foundUsers)){
                        if (!mysql_fetch_assoc($alreadyWrittenData)){
                                $extension = getUniqueExtension($conn);
                                mysql_query("CALL AddExtension('$extension', '$extension$username')");
                                //exec("asterisk -rx \"database put CW ".$extension." ENABLED\"");
                                exec("asterisk -rx \"dialplan add extension ".$extension."1,Dial,SIP/".$extension." into Friends\"");
                                $sqlConn = new SQLite3('/var/lib/asterisk/astdb.sqlite3');
                                $command = "INSERT INTO astdb VALUES('/AMPUSER/$extension/noanswer','')";
                                $sqlConn->exec($command);
                                $command = "INSERT INTO astdb VALUES('/AMPUSER/$extension/recording','')";
                                $sqlConn->exec($command);
                                $command = "INSERT INTO astdb VALUES('/AMPUSER/$extension/outboundcid','')";
                                $sqlConn->exec($command);
                                $command = "INSERT INTO astdb VALUES('/AMPUSER/$extension/cidname','$extension')";
                                $sqlConn->exec($command);
                                $command = "INSERT INTO astdb VALUES('/AMPUSER/$extension/cidnum','$extension')";
                                $sqlConn->exec($command);
                                $command = "INSERT INTO astdb VALUES('/AMPUSER/$extension/voicemail','novm')";
                                $sqlConn->exec($command);
                                $command = "INSERT INTO astdb VALUES('/AMPUSER/$extension/answermode','disabled')";
                                $sqlConn->exec($command);
                                $command = "INSERT INTO astdb VALUES('/AMPUSER/$extension/recording/in/external','dontcare')";
                                $sqlConn->exec($command);
                                $command = "INSERT INTO astdb VALUES('/AMPUSER/$extension/recording/out/external','dontcare')";
                                $sqlConn->exec($command);
                                $command = "INSERT INTO astdb VALUES('/AMPUSER/$extension/recording/in/internal','dontcare')";
                                $sqlConn->exec($command);
                                $command = "INSERT INTO astdb VALUES('/AMPUSER/$extension/recording/out/internal','dontcare')";
                                $sqlConn->exec($command);
                                $command = "INSERT INTO astdb VALUES('/AMPUSER/$extension/recording/ondemand','disabled')";
                                $sqlConn->exec($command);
                                $command = "INSERT INTO astdb VALUES('/AMPUSER/$extension/recording/priority','10')";
                                $sqlConn->exec($command);
                                $command = "INSERT INTO astdb VALUES('/CW/$extension','ENABLED')";
                                $sqlConn->exec($command);
                                $command = "INSERT INTO astdb VALUES('/DEVICE/$extension/user','$extension')";
                                $sqlConn->exec($command);
                                $command = "INSERT INTO astdb VALUES('/DEVICE/$extension/dial','SIP/$extension')";
                                $sqlConn->exec($command);
                                $command = "INSERT INTO astdb VALUES('/DEVICE/$extension/type','fixed')";
                                $sqlConn->exec($command);
                                $command = "INSERT INTO astdb VALUES('/DEVICE/$extension/default_user','$extension')";
                                $sqlConn->exec($command);
                                $command = "INSERT INTO astdb VALUES('/AMPUSER/$extension/device','$extension')";
                                $sqlConn->exec($command);
                                $command = "INSERT INTO astdb VALUES('/AMPUSER/$extension/queues/qnostate','usestate')";
                                $sqlConn->exec($command);
                                $command = "INSERT INTO astdb VALUES('/AMPUSER/$extension/language','')";
                                $sqlConn->exec($command);
                                $command = "INSERT INTO astdb VALUES('/AMPUSER/$extension/ccss/cc_agent_policy','generic')";
                                $sqlConn->exec($command);
                                $command = "INSERT INTO astdb VALUES('/AMPUSER/$extension/ccss/cc_offer_timer','30');";
                                $sqlConn->close();
                                unset($sqlConn);
                                exec("/var/lib/asterisk/bin/module_admin reload");
                                echo $extension;
                                mysql_close($conn);
                        }
                        else {
                                echo "ERROR:USRALRDYEXIST";
                        }
                }
                else
                    echo "ERROR:NUSRFOUND";
            }
            else
                echo "ERROR:CONNERR";
        }
        catch (Exception $e){
            echo "ERROR:".$e->getMessage();
        }
    }
    else
        echo "ERROR:NPARAMFND";

    function getUniqueExtension($conn){
        try{
            if ($conn){
                while (true){
                        $uExtension = mt_rand(1,1000000000);
                        if (! mysql_query("SELECT vcard FROM ofvcard WHERE vcard LIKE '%<EXTENSION>$uExtension<EXTENSION>%'", $conn))
                                return $uExtension;
                }
            }
        }
        catch (Exception $ex){}
                return int(var_dump(microtime(true)));
    }
	
	function DoReload(){
		$options = array(
			'http' => array(
			'method'  => 'POST',
			'content' => json_encode("handler=reload"),
			'header'=>  "Content-Type: application/json\r\n" .
						"Accept: application/json\r\n"
			)
		);
		$context  = stream_context_create( $options );
		$result = file_get_contents("../admin/config.php", false, $context );
	}
?>